package com.grace.mapper;

import com.grace.entity.Permission;
import com.grace.entity.Role;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface RoleMapper {
    /*
     private String id;
    private String roleName;
    private String roleDesc;
    private List<Permission> permissions;
    private List<UserInfo> userInfos;
     */

    /**
     * 根据用户id查找用户的角色表
     *
     * @param userId
     * @return
     */
    @Select("select * from role where id in(select roleId from users_role where userId=#{userId} )")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "roleName", column = "roleName"),
            @Result(property = "roleDesc", column = "roleDesc"),
            @Result(property = "permissions", column = "id", javaType = java.util.List.class, many = @Many(select = "com.grace.mapper.PermissionMapper.findPermissionByRoleId")),

    })
    public List<Role> findRoleByUserId(String userId);


    @Select("select * from role")
    List<Role> findAll();

    @Insert("insert into role(roleName,roleDesc) values(#{roleName},#{roleDesc})")
    void save(Role role);

    @Select("select * from role where id=#{roleId}")
    Role findById(String roleId);

    @Select("select * from permission where id not in(select permissionId from role_permission where roleId=#{roleId})")
    List<Permission> findByRoleIdAndOtherPers(String roleId);

    @Insert("insert into role_permission(roleId,permissionId) values(#{roleId},#{perId})")
    void addPermissionToRole(@Param("roleId") String roleId,@Param("perId") String perId);
}
